{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "快速\n"
     ]
    }
   ],
   "source": [
    "\n",
    "    import requests\n",
    "\n",
    "    if __name__ == '__main__':\n",
    "        url = \"https://spark-api-open.xf-yun.com/v1/chat/completions\"\n",
    "        data = {\n",
    "    \"max_tokens\": 4096,\n",
    "    \"top_k\": 4,\n",
    "    \"temperature\": 0.5,\n",
    "    \"messages\": [\n",
    "        {\n",
    "            \"role\": \"system\",\n",
    "            \"content\": \"你是一个非常厉害的Java程序员\"\n",
    "        },\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"生成快速排序\"\n",
    "        }\n",
    "    ],\n",
    "    \"model\": \"4.0Ultra\"\n",
    "}\n",
    "        data[\"stream\"] = True\n",
    "        header = {\n",
    "            \"Authorization\": \"Bearer zrqPrkHVkglddaWesnyR:nefnKBrBEIZxieVDomwu\"\n",
    "        }\n",
    "        response = requests.post(url, headers=header, json=data, stream=True)\n",
    "\n",
    "        # 流式响应解析示例\n",
    "        ans = \"\"\n",
    "        response.encoding = \"utf-8\"\n",
    "        for line in response.iter_lines(decode_unicode=\"utf-8\"):\n",
    "            # 使用正则提取 \"content\" 后面的内容\n",
    "            match = re.search(r'\"content\":\"(.*?)\"', line)\n",
    "            if match:\n",
    "                content = match.group(1)\n",
    "                print(content)  # 输出：快速\n",
    "                ans.join(content)\n",
    "            break\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "快速sqshunsjnq\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "data = '{\"code\":0,\"message\":\"Success\",\"sid\":\"cha000bd148@dx195f15f58dfb894532\",\"id\":\"cha000bd148@dx195f15f58dfb894532\",\"created\":1743511312,\"choices\":[{\"delta\":{\"role\":\"assistant\",\"content\":\"快速sqshunsjnq\"},\"index\":0}]}'\n",
    "\n",
    "# 使用正则提取 \"content\" 后面的内容\n",
    "match = re.search(r'\"content\":\"(.*?)\"', data)\n",
    "\n",
    "if match:\n",
    "    content = match.group(1)\n",
    "    print(content)  # 输出：快速\n",
    "else:\n",
    "    print(\"未找到 content\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://spark-api-open.xf-yun.com/v1/chat/completions\"\n",
    "data = {\n",
    "    \"max_tokens\": 4096,\n",
    "    \"top_k\": 4,\n",
    "    \"temperature\": 0.5,\n",
    "    \"messages\": [\n",
    "        {\n",
    "            \"role\": \"system\",\n",
    "            \"content\": \"你是一个非常厉害的Java程序员\"\n",
    "        },\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"生成快速排序\"\n",
    "        }\n",
    "    ],\n",
    "    \"model\": \"4.0Ultra\"\n",
    "}\n",
    "data[\"stream\"] = True\n",
    "header = {\n",
    "            \"Authorization\": \"Bearer zrqPrkHVkglddaWesnyR:nefnKBrBEIZxieVDomwu\"\n",
    "        }\n",
    "response = requests.post(url, headers=header, json=data, stream=True)\n",
    "\n",
    "        # 流式响应解析示例\n",
    "ans = \"\"\n",
    "response.encoding = \"utf-8\"\n",
    "for line in response.iter_lines(decode_unicode=\"utf-8\"):\n",
    "    # 使用正则提取 \"content\" 后面的内容\n",
    "    match = re.search(r'\"content\":\"(.*?)\"', line)\n",
    "    if match:\n",
    "        content = match.group(1)\n",
    "        print(content)  # 输出：快速\n",
    "        ans.join(content)\n",
    "    break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "d2l",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
